const axios = require('../../utils/axios');
const app = getApp();


const tabbars = [
  { name: '我的投递', value: 0 },
  { name: '我的求职', value: 1 },
];
var page = 1;
var count = -1;

Page({
  data: {
    user: app.globalData.user,
    data: {},
    requests: null,
    staffs: null,

    showAdd: false,

    tabbars,
    active: 0,
  },

  onLoad(options) {
    page = 1;
    count = -1;
    this.getData();
  },

  getData() {
    const { active, user } = this.data;
    const header = app.globalData.header;
    if(active == 0) {
      axios.get('/request', { userId: user.id }, header).then(res => {
        const { code, data, err } = res;
        console.log(res);
        if(code == 200) {
          this.setData({ requests: data });
        } else if(code == 401) {
          app.login(() => this.getData());
        } else throw err;
      }).catch(err => {
        wx.showModal({
          title: '错误',
          content: JSON.stringify(err),
          showCancel: false
        });
      });
    } else {
      const { staffs } = this.data;
      if(staffs && staffs.length == count) return;
      axios.get('/staff', { userId: user.id, page, size: 20 }, header).then(res => {
        const { code, data, total, err } = res;
        if(code == 200) {
          this.setData({ staffs: (staffs || []).concat(data) });
          page++;
          count = total;
        } else if (code == 401) {
          app.login(() => this.getData());
        } else throw err;
      }).catch(err => {
        wx.showModal({
          title: '错误',
          content: JSON.stringify(err),
          showCancel: false
        });
      });
    }
  },

  onJump(e) {
    wx.navigateTo(e.currentTarget.dataset);
  },

  onShowAdd() {
    this.setData({ showAdd: !this.data.showAdd });
  },

  onChangeTabbar(e) {
    this.setData({ active: e.detail });
    console.log([this.data.requests, this.data.staffs][e.detail]);
    if(![this.data.requests, this.data.staffs][e.detail]) {
      console.log(111)
      this.getData();
    }
  },

  onInput(e) {
    const { key } = e.currentTarget.dataset;
    const name = 'data.' + key;
    this.setData({ [name]: e.detail.value });
  },

  onChangeBoolean(e) {
    const { key } = e.currentTarget.dataset;
    var sourceData = this.data;
    const parts = key.split('.');
    parts.forEach(part => {
      sourceData = sourceData[part];
    });
    this.setData({ [key]: !sourceData });
  },
  
  onConfirm() {
    function Toast(title) {
      wx.showToast({
        title,
        icon: 'error',
        duration: 1500,
        mask: true
      });
      return false;
    }

    function check(data) {
      if(!data.jName) {
        return Toast('请输入岗位名称');
      } else if(!data.city) {
        return Toast('请输入意向城市');
      } else if(!data.salary) {
        return Toast('请输入意向薪资');
      }

      return true;
    }

    const { data, user } = this.data;
    const header = app.globalData.header;
    if(!check(data)) {
      return;
    }
    axios.post('/staff', { ...data, userId: user.id }, header).then(res => {
      const { code, data, err } = res;
      if(code == 200) {
        console.log(data);
        wx.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 1500,
          mask: true
        });
        this.setData({ showAdd: false });
        const { active } = this.data;
        if (active == 1) {
          page = 1;
          this.setData({ requests: [] });
          this.getData();
        }
      } else if(code == 401) {
        app.login(() => this.onConfirm());
      } else throw err;
    }).catch(err => {
      wx.showModal({
        title: '错误',
        content: JSON.stringify(err),
        showCancel: false
      });
    });
  },

  onReachBottom() {
    if(this.data.active) this.getData();
  }
})